Repository navigation
fix: never crash on output the console's encoding cannot represent - #104
Conversation
|
I like this, makes sense that Rich would have thought of this. Related to: #91 FYI, smpmgr is not really meant to be wrapped, since it is a user-facing CLI app. I highly recommend writing your own app with smpclient at the point you need reliability, performance, customization, etc. https://rich.readthedocs.io/en/stable/reference/console.html#rich.console.Console Please run the linters locally, rebase, and squash. |
There was a problem hiding this comment.
Pull request overview
Updates CLI progress spinners to avoid Windows charmap encoding failures.
Changes:
- Uses an ASCII-compatible spinner.
- Forces terminal rendering only when stdout is a terminal.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
79a766a to
23fd29c
Compare
|
@Filip-Dymczyk Sorry that this fell off my radar! Doing some updates to this branch now and getting it merged + release ASAP. Thanks! |
23fd29c to
df1e6ec
Compare
Rich's default spinner ("dots") is drawn in braille (U+2800-U+28FF). When
stdout's encoding cannot represent it, writing a frame raises and the
command aborts. That happens on a Windows console with the cp1252 code page
(intercreate#91), or on the pipe that a process wrapping the smpmgr executable reads
(intercreate#104):
UnicodeEncodeError: 'charmap' codec can't encode character '⠴'
in position 0: character maps to <undefined>
spinner_progress() now builds every spinner Progress: connect_with_spinner,
smp_request, and `file download`, which intercreate#104 missed. It keeps "dots" on a
UTF console and switches to Rich's ASCII "line" spinner when
`Console.options.ascii_only` is true (the encoding is not utf-*). That is
the same test Rich's own ProgressBar uses for its ASCII fallback. It still
uses Rich's global console, so RichHandler log records keep rendering
above the live spinner, and FORCE_COLOR / TTY_COMPATIBLE detection is
unchanged.
Compared with intercreate#104 as first proposed: that version forced the "line"
spinner on everyone and gave each Progress a private
`Console(force_terminal=sys.stdout.isatty())`. The private console
separated the spinner from the console RichHandler logs through, so log
lines collide with the spinner. force_terminal overrode Rich's environment
detection, and `sys.stdout.isatty()` raises when sys.stdout is None. Its
unrelated black reformat of main.py is dropped.
PEP 686 makes UTF-8 mode the default in Python 3.15, but the executables
are still built on 3.12 (build.yaml), so this still matters.
Verified: with PYTHONIOENCODING=cp1252 and stdout piped,
`smpmgr --port /dev/does-not-exist os echo hi` crashes on main with the
error above. With this change it prints
"/ Connecting to /dev/does-not-exist... error" and exits 1.
tests/test_spinner_progress.py shows Rich's default spinner raising on
cp1252, ASCII-only output on cp1252 and ascii, and braille kept on UTF-8.
Co-authored-by: Filip <filip.dymczyk@o2.pl>
Co-Authored-By: claude-opus-5-5 <noreply@anthropic.com>
|
Warning LLM Disclosure This post was authored by claude-opus-5-5 on behalf of @JPHutchins. JP asked me to review this PR and get it across the finish line. Thanks @Filip-Dymczyk for tracking this down. I rebased onto
ReproductionWith
Locally, black, isort, flake8, mypy and pytest (20 passed) all pass. This is also #91. PEP 686 makes UTF-8 the default in Python 3.15, but the executables are still built on 3.12, so the fix still matters. |
When stdout's encoding cannot represent a character smpmgr writes, Python raises and the command aborts. That happens on a Windows console with the cp1252 code page (intercreate#91), or on the pipe that a process wrapping the smpmgr executable reads (intercreate#104): UnicodeEncodeError: 'charmap' codec can't encode character '⠴' in position 0: character maps to <undefined> The first character to fail is Rich's braille spinner, but it is not the only one. A transport address, BLE name or file path in a task description can fail too (`--port /dev/☃` on cp1252), as can the "•" separator of the upload progress bars on ascii/cp437, and Rich tracebacks. Two layers: - replace_unencodable_output(), called first in the CLI callback, reconfigures stdout and stderr with errors="replace". A character the stream cannot encode is written as "?" instead of raising, on every output path at once. - spinner_progress() builds every spinner Progress: connect_with_spinner, smp_request, and `file download`, which intercreate#104 missed. It keeps "dots", but draws Rich's ASCII "line" spinner where Rich's own ProgressBar falls back to ASCII (`legacy_windows or options.ascii_only`, rich progress_bar.py). The spinner then animates instead of degrading to "?". It takes Rich's global console, so RichHandler log records still render above the live spinner, and FORCE_COLOR / TTY_COMPATIBLE detection is unchanged. Compared with intercreate#104 as first proposed: that version forced the "line" spinner on everyone and gave each Progress a private `Console(force_terminal=sys.stdout.isatty())`. The private console separated the spinner from the console RichHandler logs through, so log lines collide with the spinner. force_terminal overrode Rich's environment detection, and `sys.stdout.isatty()` raises when sys.stdout is None. Its unrelated black reformat of main.py is dropped. PEP 686 makes UTF-8 mode the default in Python 3.15, but the executables are still built on 3.12 (build.yaml), so this still matters. Building them in UTF-8 mode would be the other route. Verified, with PYTHONIOENCODING=cp1252 and stdout piped: `smpmgr --port /dev/does-not-exist os echo hi` crashes on main with the error above, and `--port /dev/☃` crashes on '☃'. With this change they print "/ Connecting to /dev/does-not-exist... error" and "/ Connecting to /dev/?... error", and exit 1. In tests: - test_cp1252_output_replaces_what_it_cannot_encode drives the CLI with a cp1252 stdout. With replace_unencodable_output stubbed out, the same invocation raises UnicodeEncodeError. - tests/test_spinner_progress.py checks for ASCII-only output on cp1252, ascii and legacy Windows consoles, and braille kept on UTF-8. Co-authored-by: Filip <filip.dymczyk@o2.pl> Co-Authored-By: claude-opus-5-5 <noreply@anthropic.com>
df1e6ec to
cbccdd3
Compare
Warning
LLM Disclosure
This description was rewritten by claude-opus-5-5 on behalf of @JPHutchins, who asked me to review this PR and get it across the finish line. @Filip-Dymczyk's original report is kept below.
Context (from @Filip-Dymczyk): wrapping the downloaded
.exein Python crashed withUnicodeEncodeError: 'charmap' codec can't encode character '⠦'. Rich's braille spinner can't be written to a cp1252 stdout, which is what a Windows pipe uses. The same crash was reported in #91.Changes
replace_unencodable_output(), called first in the CLI callback, sets stdout and stderr to write?for any character they can't encode, instead of raising. This covers every output path: spinners, task descriptions holding non-Latin addresses or paths, the•in upload bars on ascii/cp437, and Rich tracebacks.spinner_progress()builds all three spinners (connect, request,file download). It keepsdots, but switches to Rich's ASCIIlinespinner where Rich's own progress bar falls back to ASCII (legacy_windows or ascii_only). It uses Rich's global console, so log lines still render above the spinner.Verification
With
PYTHONIOENCODING=cp1252and stdout piped:main--port /dev/does-not-exist os echo hiUnicodeEncodeError … '⠴'/ Connecting to /dev/does-not-exist... error, exit 1--port /dev/☃ os echo hiUnicodeEncodeError … '☃'/ Connecting to /dev/?... error, exit 1test_cp1252_output_replaces_what_it_cannot_encodedrives the CLI with a cp1252 stdout. Withreplace_unencodable_outputstubbed out, the same invocation raisesUnicodeEncodeError.tests/test_spinner_progress.pychecks for ASCII-only output on cp1252, ascii and legacy Windows consoles, and braille kept on UTF-8.PEP 686 makes UTF-8 the default in Python 3.15, but the executables are still built on 3.12.
Closes #91 (already closed; same root cause).
🤖 Generated with Claude Code